/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WISHMASTER_TASKHUB_H
#define WISHMASTER_TASKHUB_H

#include <QObject>
#include <QString>
#include <QList>
#include <QMutex>
#include <QMutexLocker>
#include "misc.h"

#include "task.h"

namespace Wishmaster
{
	class WISHMASTER_EXPORT Taskhub : public QObject
	{
		Q_OBJECT
	public:
		Taskhub();
		virtual ~Taskhub();

		void queueTask(const TaskPtr& t);
		void queueTaskFirst(const TaskPtr& t);
		void taskPercentage(int id, int percentage);

		void cancelTask(int id);

		int taskCount();
		TaskPtr taskByIndex(int index);
		TaskPtr taskById(int id);
	
	protected:
		void update();

	signals:
		void totalProgress(int percentage);
		void taskProgress(int id, int percentage);
		void taskDone(TaskPtr t);

	protected slots:
		void slot_taskDone(int id);
		void slot_taskProgress(int id, int done, int total);

	private:
		void queue(const TaskPtr& t, bool first);
		QList<TaskPtr> m_tasks;
		QList<TaskPtr> m_pending;
		QList<TaskPtr> m_active;
		QMutex m_mutex;
		int m_activeQueueLength;
		int m_totalProgress;
		int m_done;
		int m_total;
	};
}

#endif
